#!/bin/bash
#set -eux

APP_DIR=$(cd `dirname $0`/../; pwd)
OS_TYPE=""
. $APP_DIR/scripts/common
ACTION=$1

show_help()
{
    echo ""
    echo "Usage: ./all {start|stop|restart}"
    echo ""
}

if [[ $# -ne 1 ]] ; then
    show_help
    exit 1
fi

function start()
{
    echo "------------nats-server--------------"
    $APP_DIR/scripts/deps start nats-server

    echo "------------redis--------------"
    $APP_DIR/scripts/deps start redis

    echo "------------islb--------------"
    $APP_DIR/scripts/service start islb

    echo "------------signal--------------"
    $APP_DIR/scripts/service start signal

    echo "------------app-biz--------------"
    $APP_DIR/scripts/service start app-biz

    echo "------------sfu--------------"
    $APP_DIR/scripts/service start sfu

    echo "------------avp--------------"
    $APP_DIR/scripts/service start avp

    echo "--------------------------"
}

function stop()
{
    echo "------------biz--------------"
    $APP_DIR/scripts/service stop app-biz

    echo "------------signal--------------"
    $APP_DIR/scripts/service stop signal

    echo "------------islb--------------"
    $APP_DIR/scripts/service stop islb

    echo "------------sfu--------------"
    $APP_DIR/scripts/service stop sfu

    echo "------------avp--------------"
    $APP_DIR/scripts/service stop avp

    echo "------------nats-server--------------"
    $APP_DIR/scripts/deps stop nats-server

    echo "------------redis--------------"
    $APP_DIR/scripts/deps stop redis

    echo "--------------------------"
}

if [[ "$ACTION" != "start" && "$ACTION" != "stop" && "$ACTION" != "restart" ]]; then
    show_help
    exit 1
fi

if [[ "$ACTION" == "start" ]]; then
    start
    exit 0
fi

if [[ "$ACTION" == "stop" ]]; then
    stop
    exit 0
fi

if [[ "$ACTION" == "restart" ]]; then
    stop
    start
    exit 0
fi


